package com.ujiuye.dao;

import com.ujiuye.enity.PageUtils;
import com.ujiuye.enity.Student;

import java.util.List;

public class StuDao extends BaseDao<Student> {
    public List<Student> showAll() {
        String sql = "select * from student";
        return queryMore(sql, Student.class);
    }

    //定义删除信息方法
    public int delete(String id) {
        String sql = "delete from student where sid = ?";
        return update(sql, id);
    }

    //定义添加学生信息方法
    public int addStu(Student s) {
        String sql = "insert into student(sname,age,sbir,email,sex,spic) values(?,?,?,?,?,?)";
        Object[] params = {s.getSname(), s.getAge(), s.getSbir(), s.getEmail(), s.getSex(), s.getSpic()};
        return update(sql, params);
    }

    //定义修改学生信息的方法
    public int update(Student s) {
        String sql = "update student set sname=?,age=?,sbir=?,email=?,sex=?,spic=? where sid=?";
        Object[] params = {s.getSname(), s.getAge(), s.getSbir(), s.getEmail(), s.getSex(), s.getSpic(), s.getSid()};
        return update(sql, params);
    }

    //定义根据id查询学生信息的方法
    public Student qStu(String id) {
        String sql = "select * from student where sid = ?";
        return queryOne(sql, Student.class, id);
    }

    //分页查询
    public List<Student> queryStuPage(PageUtils pu) {
        String sql = "select * from student limit ?,?";
        return queryMore(sql, Student.class, pu.getStartIndex(), pu.getPageSize());
    }

    //查询总记录数
    public int queryNum() {
        String sql = "select count(*) from student";
        return queryScar(sql).intValue();
    }
    //模糊查询
    public List<Student> queryStuLike(String name,String sex,PageUtils pu){
        StringBuffer sql=new StringBuffer("select * from student where 1=1 ");
        if(name!=null&&!"".equals(name)){
            sql.append(" and sname like '%"+name+"%' ");
        }
        if("0".equals(sex)||"1".equals(sex)){
            sql.append(" and sex = "+sex);
        }
        sql.append(" limit ?,?");
        System.out.println(sql.toString());
        return queryMore(sql.toString(),Student.class,pu.getStartIndex(),pu.getPageSize());
    }
    //模糊查询记录统计
    public int queryLikeNum(String name, String sex) {
        StringBuffer sql = new StringBuffer("select count(*) from student where 1=1 ");
        if (name != null && !"".equals(name)) {
            sql.append(" and sname like '%" + name + "%' ");
        }
        if ("0".equals(sex) || "1".equals(sex)) {
            sql.append(" and sex = " + sex);
        }
        return queryScar(sql.toString()).intValue();
    }
}
